home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Przegladarki internetowe / Mozilla Seamonkey 1.0.5 pl / seamonkey-1.0.5.pl-PL.win32.installer.exe / BROWSER.XPI / bin / chrome / toolkit.jar / content / global / printdialog.js < prev    next >
Encoding:
JavaScript  |  2004-12-23  |  16.9 KB  |  472 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Netscape Communications Corporation.
  20.  * Portions created by the Initial Developer are Copyright (C) 1998
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *   Masaki Katakai <katakai@japan.sun.com>
  25.  *   Jessica Blanco <jblanco@us.ibm.com>
  26.  *   Asko Tontti <atontti@cc.hut.fi>
  27.  *   Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
  28.  *   Peter Weilbacher <mozilla@weilbacher.org>
  29.  *
  30.  * Alternatively, the contents of this file may be used under the terms of
  31.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  32.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  33.  * in which case the provisions of the GPL or the LGPL are applicable instead
  34.  * of those above. If you wish to allow use of your version of this file only
  35.  * under the terms of either the GPL or the LGPL, and not to allow others to
  36.  * use your version of this file under the terms of the MPL, indicate your
  37.  * decision by deleting the provisions above and replace them with the notice
  38.  * and other provisions required by the GPL or the LGPL. If you do not delete
  39.  * the provisions above, a recipient may use your version of this file under
  40.  * the terms of any one of the MPL, the GPL or the LGPL.
  41.  *
  42.  * ***** END LICENSE BLOCK ***** */
  43.  
  44. var dialog;
  45. var printService       = null;
  46. var printOptions       = null;
  47. var gOriginalNumCopies = 1;
  48.  
  49. var paramBlock;
  50. var gPrefs             = null;
  51. var gPrintSettings     = null;
  52. var gWebBrowserPrint   = null;
  53. var gPrintSetInterface = Components.interfaces.nsIPrintSettings;
  54. var doDebug            = false;
  55.  
  56. //---------------------------------------------------
  57. function initDialog()
  58. {
  59.   dialog = new Object;
  60.  
  61.   dialog.propertiesButton = document.getElementById("properties");
  62.   dialog.descText         = document.getElementById("descText");
  63.  
  64.   dialog.printrangeGroup = document.getElementById("printrangeGroup");
  65.   dialog.allpagesRadio   = document.getElementById("allpagesRadio");
  66.   dialog.rangeRadio      = document.getElementById("rangeRadio");
  67.   dialog.selectionRadio  = document.getElementById("selectionRadio");
  68.   dialog.frompageInput   = document.getElementById("frompageInput");
  69.   dialog.frompageLabel   = document.getElementById("frompageLabel");
  70.   dialog.topageInput     = document.getElementById("topageInput");
  71.   dialog.topageLabel     = document.getElementById("topageLabel");
  72.  
  73.   dialog.numCopiesInput  = document.getElementById("numCopiesInput");  
  74.  
  75.   dialog.printframeGroup      = document.getElementById("printframeGroup");
  76.   dialog.aslaidoutRadio       = document.getElementById("aslaidoutRadio");
  77.   dialog.selectedframeRadio   = document.getElementById("selectedframeRadio");
  78.   dialog.eachframesepRadio    = document.getElementById("eachframesepRadio");
  79.   dialog.printframeGroupLabel = document.getElementById("printframeGroupLabel");
  80.  
  81.   dialog.fileCheck       = document.getElementById("fileCheck");
  82.   dialog.printerLabel    = document.getElementById("printerLabel");
  83.   dialog.printerList     = document.getElementById("printerList");
  84.  
  85.   dialog.printButton     = document.documentElement.getButton("accept");
  86.  
  87.   // <data> element
  88.   dialog.fpDialog        = document.getElementById("fpDialog");
  89.  
  90.   dialog.enabled         = false;
  91. }
  92.  
  93. //---------------------------------------------------
  94. function checkInteger(element)
  95. {
  96.   var value = element.value;
  97.   if (value && value.length > 0) {
  98.     value = value.replace(/[^0-9]/g,"");
  99.     if (!value) value = "";
  100.     element.value = value;
  101.   }
  102.   if (!value || value < 1 || value > 999)
  103.     dialog.printButton.setAttribute("disabled","true");
  104.   else
  105.     dialog.printButton.removeAttribute("disabled");
  106. }
  107.  
  108. //---------------------------------------------------
  109. function stripTrailingWhitespace(element)
  110. {
  111.   var value = element.value;
  112.   value = value.replace(/\s+$/,"");
  113.   element.value = value;
  114. }
  115.  
  116. //---------------------------------------------------
  117. function getPrinterDescription(printerName)
  118. {
  119.   var s = "";
  120.  
  121.   try {
  122.     /* This may not work with non-ASCII test (see bug 235763 comment #16) */
  123.     s = gPrefs.getCharPref("print.printer_" + printerName + ".printer_description")
  124.   } catch(e) {
  125.   }
  126.     
  127.   return s;
  128. }
  129.  
  130. //---------------------------------------------------
  131. function listElement(aListElement)
  132.   {
  133.     this.listElement = aListElement;
  134.   }
  135.  
  136. listElement.prototype =
  137.   {
  138.     clearList:
  139.       function ()
  140.         {
  141.           // remove the menupopup node child of the menulist.
  142.           var popup = this.listElement.firstChild;
  143.           if (popup) {
  144.             this.listElement.removeChild(popup);
  145.           }
  146.         },
  147.  
  148.     appendPrinterNames: 
  149.       function (aDataObject) 
  150.         { 
  151.           var list = document.getElementById("printerList"); 
  152.           var strDefaultPrinterName = "";
  153.           var printerName;
  154.  
  155.           // build popup menu from printer names
  156.           while (aDataObject.hasMoreElements()) {
  157.             printerName = aDataObject.getNext();
  158.             printerName = printerName.QueryInterface(Components.interfaces.nsISupportsString);
  159.             var printerNameStr = printerName.toString();
  160.             if (strDefaultPrinterName == "")
  161.                strDefaultPrinterName = printerNameStr;
  162.  
  163.             list.appendItem(printerNameStr, printerNameStr, getPrinterDescription(printerNameStr));
  164.           }
  165.           if (strDefaultPrinterName != "") {
  166.             this.listElement.removeAttribute("disabled");
  167.           } else {
  168.             var stringBundle = srGetStrBundle("chrome://global/locale/printing.properties");
  169.             this.listElement.setAttribute("value", strDefaultPrinterName);
  170.             this.listElement.setAttribute("label", stringBundle.GetStringFromName("noprinter"));
  171.  
  172.             // disable dialog
  173.             this.listElement.setAttribute("disabled", "true");
  174.             dialog.printerLabel.setAttribute("disabled","true");
  175.             dialog.propertiesButton.setAttribute("disabled","true");
  176.             dialog.fileCheck.setAttribute("disabled","true");
  177.             dialog.printButton.setAttribute("disabled","true");
  178.           }
  179.  
  180.           return strDefaultPrinterName;
  181.         } 
  182.   };
  183.  
  184. //---------------------------------------------------
  185. function getPrinters()
  186. {
  187.   var printerEnumerator = printOptions.availablePrinters();
  188.  
  189.   var selectElement = new listElement(dialog.printerList);
  190.   selectElement.clearList();
  191.   var strDefaultPrinterName = selectElement.appendPrinterNames(printerEnumerator);
  192.  
  193.   selectElement.listElement.value = strDefaultPrinterName;
  194.  
  195.   // make sure we load the prefs for the initially selected printer
  196.   setPrinterDefaultsForSelectedPrinter();
  197. }
  198.  
  199.  
  200. //---------------------------------------------------
  201. // update gPrintSettings with the defaults for the selected printer
  202. function setPrinterDefaultsForSelectedPrinter()
  203. {
  204.   gPrintSettings.printerName = dialog.printerList.value;
  205.   
  206.   dialog.descText.value = getPrinterDescription(gPrintSettings.printerName);
  207.   
  208.   // First get any defaults from the printer 
  209.   printService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
  210.   
  211.   // now augment them with any values from last time
  212.   printService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSetInterface.kInitSaveAll);
  213.   
  214.   if (doDebug) {
  215.     dump("setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', paperName='"+gPrintSettings.paperName+"'\n");
  216.   }
  217. }
  218.  
  219. //---------------------------------------------------
  220. function displayPropertiesDialog()
  221. {
  222.   gPrintSettings.numCopies = dialog.numCopiesInput.value;
  223.   try {
  224.     var printingPromptService = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  225.                                                  .getService(Components.interfaces.nsIPrintingPromptService);
  226.     if (printingPromptService) {
  227.       printingPromptService.showPrinterProperties(null, dialog.printerList.value, gPrintSettings);
  228.       dialog.numCopiesInput.value = gPrintSettings.numCopies;
  229.     }
  230.   } catch(e) {
  231.     dump("problems getting printingPromptService\n");
  232.   }
  233. }
  234.  
  235. //---------------------------------------------------
  236. function doPrintRange(inx)
  237. {
  238.   if (inx == 1) {
  239.     dialog.frompageInput.removeAttribute("disabled");
  240.     dialog.frompageLabel.removeAttribute("disabled");
  241.     dialog.topageInput.removeAttribute("disabled");
  242.     dialog.topageLabel.removeAttribute("disabled");
  243.   } else {
  244.     dialog.frompageInput.setAttribute("disabled","true");
  245.     dialog.frompageLabel.setAttribute("disabled","true");
  246.     dialog.topageInput.setAttribute("disabled","true");
  247.     dialog.topageLabel.setAttribute("disabled","true");
  248.   }
  249. }
  250.  
  251. //---------------------------------------------------
  252. function loadDialog()
  253. {
  254.   var print_copies        = 1;
  255.   var print_selection_radio_enabled = false;
  256.   var print_frametype     = gPrintSetInterface.kSelectedFrame;
  257.   var print_howToEnableUI = gPrintSetInterface.kFrameEnableNone;
  258.   var print_tofile        = "";
  259.  
  260.   try {
  261.     gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  262.  
  263.     printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"];
  264.     if (printService) {
  265.       printService = printService.getService();
  266.       if (printService) {
  267.         printService = printService.QueryInterface(Components.interfaces.nsIPrintSettingsService);
  268.         printOptions = printService.QueryInterface(Components.interfaces.nsIPrintOptions);
  269.       }
  270.     }
  271.   } catch(e) {}
  272.  
  273.   // Note: getPrinters sets up the PrintToFile radio buttons and initalises gPrintSettings
  274.   getPrinters();
  275.  
  276.   if (gPrintSettings) {
  277.     print_tofile        = gPrintSettings.printToFile;
  278.     gOriginalNumCopies  = gPrintSettings.numCopies;
  279.  
  280.     print_copies        = gPrintSettings.numCopies;
  281.     print_frametype     = gPrintSettings.printFrameType;
  282.     print_howToEnableUI = gPrintSettings.howToEnableFrameUI;
  283.     print_selection_radio_enabled = gPrintSettings.GetPrintOptions(gPrintSetInterface.kEnableSelectionRB);
  284.   }
  285.  
  286.   if (doDebug) {
  287.     dump("loadDialog*********************************************\n");
  288.     dump("print_tofile            "+print_tofile+"\n");
  289.     dump("print_frame             "+print_frametype+"\n");
  290.     dump("print_howToEnableUI     "+print_howToEnableUI+"\n");
  291.     dump("selection_radio_enabled "+print_selection_radio_enabled+"\n");
  292.   }
  293.  
  294.   dialog.printrangeGroup.selectedItem = dialog.allpagesRadio;
  295.   if (print_selection_radio_enabled) {
  296.     dialog.selectionRadio.removeAttribute("disabled");
  297.   } else {
  298.     dialog.selectionRadio.setAttribute("disabled","true");
  299.   }
  300.   doPrintRange(dialog.rangeRadio.selected);
  301.   dialog.frompageInput.value  = 1;
  302.   dialog.topageInput.value    = 1;
  303.   dialog.numCopiesInput.value = print_copies;
  304.  
  305.   if (doDebug) {
  306.     dump("print_howToEnableUI: "+print_howToEnableUI+"\n");
  307.   }
  308.  
  309.   // print frame
  310.   if (print_howToEnableUI == gPrintSetInterface.kFrameEnableAll) {
  311.     dialog.aslaidoutRadio.removeAttribute("disabled");
  312.  
  313.     dialog.selectedframeRadio.removeAttribute("disabled");
  314.     dialog.eachframesepRadio.removeAttribute("disabled");
  315.     dialog.printframeGroupLabel.removeAttribute("disabled");
  316.  
  317.     // initialize radio group
  318.     dialog.printframeGroup.selectedItem = dialog.selectedframeRadio;
  319.  
  320.   } else if (print_howToEnableUI == gPrintSetInterface.kFrameEnableAsIsAndEach) {
  321.     dialog.aslaidoutRadio.removeAttribute("disabled");       //enable
  322.  
  323.     dialog.selectedframeRadio.setAttribute("disabled","true"); // disable
  324.     dialog.eachframesepRadio.removeAttribute("disabled");       // enable
  325.     dialog.printframeGroupLabel.removeAttribute("disabled");    // enable
  326.  
  327.     // initialize
  328.     dialog.printframeGroup.selectedItem = dialog.eachframesepRadio;
  329.  
  330.   } else {
  331.     dialog.aslaidoutRadio.setAttribute("disabled","true");
  332.     dialog.selectedframeRadio.setAttribute("disabled","true");
  333.     dialog.eachframesepRadio.setAttribute("disabled","true");
  334.     dialog.printframeGroupLabel.setAttribute("disabled","true");
  335.   }
  336. }
  337.  
  338. //---------------------------------------------------
  339. function onLoad()
  340. {
  341.   // Init dialog.
  342.   initDialog();
  343.  
  344.   // param[0]: nsIPrintSettings object
  345.   // param[1]: container for return value (1 = print, 0 = cancel)
  346.  
  347.   gPrintSettings   = window.arguments[0].QueryInterface(gPrintSetInterface);
  348.   gWebBrowserPrint = window.arguments[1].QueryInterface(Components.interfaces.nsIWebBrowserPrint);
  349.   paramBlock       = window.arguments[2].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  350.  
  351.   // default return value is "cancel"
  352.   paramBlock.SetInt(0, 0);
  353.  
  354.   loadDialog();
  355. }
  356.  
  357. //---------------------------------------------------
  358. function onAccept()
  359. {
  360.   if (gPrintSettings != null) {
  361.     var print_howToEnableUI = gPrintSetInterface.kFrameEnableNone;
  362.  
  363.     // save these out so they can be picked up by the device spec
  364.     gPrintSettings.printerName = dialog.printerList.value;
  365.     print_howToEnableUI        = gPrintSettings.howToEnableFrameUI;
  366.     gPrintSettings.printToFile = dialog.fileCheck.checked;
  367.  
  368.     if (gPrintSettings.printToFile)
  369.       if (!chooseFile())
  370.         return false;
  371.  
  372.     if (dialog.allpagesRadio.selected) {
  373.       gPrintSettings.printRange = gPrintSetInterface.kRangeAllPages;
  374.     } else if (dialog.rangeRadio.selected) {
  375.       gPrintSettings.printRange = gPrintSetInterface.kRangeSpecifiedPageRange;
  376.     } else if (dialog.selectionRadio.selected) {
  377.       gPrintSettings.printRange = gPrintSetInterface.kRangeSelection;
  378.     }
  379.     gPrintSettings.startPageRange = dialog.frompageInput.value;
  380.     gPrintSettings.endPageRange   = dialog.topageInput.value;
  381.     gPrintSettings.numCopies      = dialog.numCopiesInput.value;
  382.  
  383.     var frametype = gPrintSetInterface.kNoFrames;
  384.     if (print_howToEnableUI != gPrintSetInterface.kFrameEnableNone) {
  385.       if (dialog.aslaidoutRadio.selected) {
  386.         frametype = gPrintSetInterface.kFramesAsIs;
  387.       } else if (dialog.selectedframeRadio.selected) {
  388.         frametype = gPrintSetInterface.kSelectedFrame;
  389.       } else if (dialog.eachframesepRadio.selected) {
  390.         frametype = gPrintSetInterface.kEachFrameSep;
  391.       } else {
  392.         frametype = gPrintSetInterface.kSelectedFrame;
  393.       }
  394.     }
  395.     gPrintSettings.printFrameType = frametype;
  396.     if (doDebug) {
  397.       dump("onAccept*********************************************\n");
  398.       dump("frametype      "+frametype+"\n");
  399.       dump("numCopies      "+gPrintSettings.numCopies+"\n");
  400.       dump("printRange     "+gPrintSettings.printRange+"\n");
  401.       dump("printerName    "+gPrintSettings.printerName+"\n");
  402.       dump("startPageRange "+gPrintSettings.startPageRange+"\n");
  403.       dump("endPageRange   "+gPrintSettings.endPageRange+"\n");
  404.       dump("printToFile    "+gPrintSettings.printToFile+"\n");
  405.     }
  406.   }
  407.  
  408.   var saveToPrefs = false;
  409.  
  410.   saveToPrefs = gPrefs.getBoolPref("print.save_print_settings");
  411.  
  412.   if (saveToPrefs && printService != null) {
  413.     var flags = gPrintSetInterface.kInitSavePaperSizeType  | 
  414.                 gPrintSetInterface.kInitSavePaperSizeUnit  |
  415.                 gPrintSetInterface.kInitSavePaperWidth     | 
  416.                 gPrintSetInterface.kInitSavePaperHeight    |
  417.                 gPrintSetInterface.kInitSavePaperName      | 
  418.                 gPrintSetInterface.kInitSaveColorSpace     |
  419.                 gPrintSetInterface.kInitSaveInColor        |
  420.                 gPrintSetInterface.kInitSaveResolutionName |
  421.                 gPrintSetInterface.kInitSaveDownloadFonts  |
  422.                 gPrintSetInterface.kInitSavePrintCommand   |
  423.                 gPrintSetInterface.kInitSaveShrinkToFit    |
  424.                 gPrintSetInterface.kInitSaveScaling;
  425.     printService.savePrintSettingsToPrefs(gPrintSettings, true, flags);
  426.   }
  427.  
  428.   // set return value to "print"
  429.   if (paramBlock) {
  430.     paramBlock.SetInt(0, 1);
  431.   } else {
  432.     dump("*** FATAL ERROR: No paramBlock\n");
  433.   }
  434.  
  435.   return true;
  436. }
  437.  
  438. //---------------------------------------------------
  439. function onCancel()
  440. {
  441.   // set return value to "cancel"
  442.   if (paramBlock) {
  443.     paramBlock.SetInt(0, 0);
  444.   } else {
  445.     dump("*** FATAL ERROR: No paramBlock\n");
  446.   }
  447.  
  448.   return true;
  449. }
  450.  
  451. //---------------------------------------------------
  452. const nsIFilePicker = Components.interfaces.nsIFilePicker;
  453. function chooseFile()
  454. {
  455.   try {
  456.     var fp = Components.classes["@mozilla.org/filepicker;1"]
  457.              .createInstance(nsIFilePicker);
  458.     fp.init(window, dialog.fpDialog.getAttribute("label"), nsIFilePicker.modeSave);
  459.     fp.appendFilters(nsIFilePicker.filterAll);
  460.     if (fp.show() != Components.interfaces.nsIFilePicker.returnCancel &&
  461.         fp.file && fp.file.path) {
  462.       gPrintSettings.toFileName = fp.file.path;
  463.       return true;
  464.     }
  465.   } catch(ex) {
  466.     dump(ex);
  467.   }
  468.  
  469.   return false;
  470. }
  471.  
  472.